<template>
  <div>
    <!-- 1.0 面包屑导航 -->
    <div class="contentStyle">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>购物商城</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 2.0 全选、新增、删除按钮及搜索框 -->
    <div class="contentStyle">
      <el-row>
        <el-col style="margin-top:5px;" :span="6">
          <!-- <el-button size="small" type="primary">全选</el-button> -->
          <router-link to="/admin/goodsadd">
            <el-button size="small" type="success">新增</el-button>
          </router-link>
          <el-button size="small" @click="deleteGoods" type="danger">删除</el-button>
        </el-col>

        <el-col :offset="12" :span="6">
          <el-input width="300" placeholder="请输入标题查询" v-model="keyword">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsListData"></el-button>
          </el-input>
          <!-- <el-input width="300" icon="icon-search" :on-icon-click="getGoodsListData" placeholder="请输入标题查询" v-model="keyword"> 
          </el-input>-->
        </el-col>
      </el-row>
    </div>

    <!-- 3.0 商品列表 -->
    <div>
      <el-table ref="multipleTable" :data="goodsList" tooltip-effect="dark" @select="selectRow" @select-all="selectAll" style="width: 100%">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="标题">
          <template slot-scope="data">
            <el-tooltip placement="right-start">
              <div slot="content">
                <img width="200" height="200" :src="data.row.imgurl">
              </div>
              <router-link :to="'/admin/goodsedit/'+data.row.id">{{data.row.title}}</router-link>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="categoryname" label="所属类别" width="100">
        </el-table-column>
        <el-table-column prop="stock_quantity" label="库 存" width="80">
        </el-table-column>
        <el-table-column prop="market_price" label="市场价" width="80">
        </el-table-column>
        <el-table-column prop="sell_price" label="销售价" width="80">
        </el-table-column>
        <el-table-column label="属 性" width="80">
          <template slot-scope="scope">
            <i :class="['el-icon-picture',scope.row.is_slide == 1 ? '' : 'unlight']"></i>&nbsp;
            <i :class="['el-icon-upload',scope.row.is_top==1 ? '':'unlight']"></i>
          </template>
        </el-table-column>
        <el-table-column label="操 作" width="80">
          <template slot-scope="scope">
            <router-link :to="'/admin/goodsedit/'+scope.row.id">修改</router-link>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 4.0 分页条 -->
    <div>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPageIndex" :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
      </el-pagination>
    </div>
  </div>
</template>

<style scoped>
  .contentStyle {
    border-bottom: 1px solid #eeeeee;
    padding: 6px;
    margin: 3px;
    border-radius: 3px;
  }

  .unlight {
    color: rgba(0, 0, 0, 0.2);
  }
</style>


<script>
  export default {
    data() {
      return {
        currentPageIndex: 1, //当前页码
        pageSize: 10, //每页多少条
        totalCount: 0, //总共多少条数据
        keyword: "", //搜索关键字
        goodsList: [], //商品列表
        isSelectAll: false,//是否全选
        delIds: [] //需要删除的id数组
      };
    },
    created() {
      this.getGoodsListData();
    },
    methods: {
      //每页请求的数量发生变化了
      handleSizeChange(size) {
        this.pageSize = size;

        //重新获取
        this.getGoodsListData();
      },
      handleCurrentChange(currentPageIndex) {
        this.currentPageIndex = currentPageIndex;

        //重新获取
        this.getGoodsListData();
      },
      //获取商品列表数据
      getGoodsListData() {
        const url = `/admin/goods/getlist/?pageIndex=${this
          .currentPageIndex}&pageSize=${this.pageSize}&searchvalue=${this
            .keyword}`;

        // console.log(url)

        this.$axios.get(url).then(response => {
          //赋值给商品列表数组
          this.goodsList = response.data.message;

          this.totalCount = response.data.totalcount;
        });
      },
      //选中某一行商品
      selectRow(selection, row) {
        const index = this.delIds.findIndex((value, index, arr) => {
          return value == row.id
        })

        if (index > -1) {//原先已经存在
          this.delIds.splice(index, 1)
        } else {
          this.delIds.push(row.id)
        }
      },
      //全选商品
      selectAll(selection) {
        if (selection.length == 0) {
          this.delIds = []
        } else {
          selection.forEach(item => {
            this.delIds.push(item.id)
          });
        }
      },
      //删除商品
      deleteGoods() {
        if (this.delIds.length <= 0) {
          this.$message.error("请选择一个要删除的商品!")
          return
        }

        const url = `admin/goods/del/` + this.delIds.join(',')

        this.$confirm('是否删除所选择的商品?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.get(url).then(response => {
            if (response.data.status == 1) {
              this.$message.error(response.data.message)
              return
            }

            this.getGoodsListData()
          })
        }).catch(() => {
          console.log("no")
        });


      }
    }
  };
</script>